﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// returns the alpha channel of image. 
    /// If alpha channel is 255 in every pixel you will get a complete white pic
    /// if alpha channel is 0 it will be completely black...
    /// if it is different you will get a grayscale pic
    /// </summary>
    public class AlphaChannel : BaseImageFilter
    {
        public delegate Color AlphaChannelDelegate(Color _color);
        protected AlphaChannelDelegate m_AlphaChannelDelegate = null;

        /// <summary>
        /// default
        /// </summary>
        public AlphaChannel()
        {
            m_AlphaChannelDelegate = new AlphaChannelDelegate(
                delegate(Color _color)
                {
                    int nGray = _color.A;
                    return Color.FromArgb(nGray, nGray, nGray);
                });
        }

        public AlphaChannelDelegate AlphaChannelCalculation
        {
            get { return m_AlphaChannelDelegate; }
            set { m_AlphaChannelDelegate = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_alphaChannel"></param>
        internal AlphaChannel(AlphaChannel _alphaChannel)
        {
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;
            Color color = Color.Transparent;
            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    _bitmap.SetPixel(x, y, m_AlphaChannelDelegate(color));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new AlphaChannel(this);
        }
        
    }
}
